import { _decorator, Component, director, Label, Node, Sprite } from 'cc';
import { HomeBuildCfg } from '../../config/home_build';
import { Global } from '../../Global';
import { Tools } from '../../Tools';
import { LvLStorage } from '../../LvLStorage';
import { events } from '../../Enums';
const { ccclass, property } = _decorator;

@ccclass('HouseElement')
export class HouseElement extends Component {
    @property(Sprite)
    ele_icon: Sprite = null;
    @property(Label)
    ele_cost_coin: Label = null;
    @property(Node)
    ele_stage:Node[] = [];
    @property(Sprite)
    quan_icon: Sprite = null;
    @property(Sprite)
    coin_icon: Sprite = null;

    private house_id:number = 0;
    private current_coins:number = 0;
    private need_coins:number = 0;
    private need_quan:number = 0;
    start() {

    }

    public initData(houseId:number,ele:number,stage:number){
        this.house_id = houseId;
        let cfg = HomeBuildCfg[houseId];
        if(!cfg){
            return;
        }

        this.initBuildStage(stage);
        

        console.log(`this.ele.stage:${stage}`);
        if(stage == 5){
            this.need_coins = 0;
            this.need_quan = 0;
            this.node.getChildByName("money").active = false;
            this.node.getChildByName("finish").active = true;
        }else{
            this.need_coins = cfg.element[ele].step[stage+1].coin;
            this.need_quan = cfg.element[ele].step[stage+1].quan;
        }
        
        this.current_coins = Global.cur_coins;
        
        if(this.need_quan > 0){
            this.quan_icon.node.active = true;
            this.coin_icon.node.active = false;
            this.ele_cost_coin.string = Tools.formatNumberToKM(this.need_quan);
        }else{
            this.quan_icon.node.active = false;
            this.coin_icon.node.active = true;
            this.ele_cost_coin.string = Tools.formatNumberToKM(this.need_coins);
        }
        
    }

    private initBuildStage(stage:number){
        for(let i = 0;i<this.ele_stage.length;i++){
            if(i<=stage-1){
                this.ele_stage[i].getChildByName("finish")!.active = true;
            }else{
                this.ele_stage[i].getChildByName("finish")!.active = false;
            }
        }

        if(stage == 5){
            this.node.getChildByName("money").active = false;
            this.node.getChildByName("finish").active = true;
        }
        
    }

    public onClickInstruct(event:Event,idx:string){
        if(this.need_quan > 0){

        }else if(this.need_coins > 0){
            // if(this.current_coins < this.need_coins){
            //     Global.tips_action.show("金币不足，提升建筑可收获更多金币");
            //     return;
            // }
            let _idx:number = Number(idx);
            let house_info = LvLStorage.get_house_info();
            house_info["stage"][_idx] += 1;
            LvLStorage.record_house_info(house_info);
            this.initBuildStage(house_info["stage"][_idx]);
            let data:any = {
                "id":this.house_id,
                "ele":_idx,
                "stage":house_info["stage"][_idx]
            }
            director.emit(events.build_instruct,data);
        }
    }

    update(deltaTime: number) {
        
    }
}

